<?php

/**
 * Created by PhpStorm.
 * User: lovel
 * Date: 2017/10/24
 * Time: 0:56
 */
class GetInfo_model extends MY_Model
{
    function get_info($body)
    {
        if (empty($body)) {
            $sql = /** @lang text */
                "SELECT a.id,a.name,b.`name` AS sc,c.`name` AS g,a.`status` AS s FROM ws_baseinfo AS a LEFT JOIN ws_dict_school AS b ON a.`school`=b.`id` LEFT JOIN ws_dict_grade AS c ON a.`grade`=c.`id`";
            $query = $this->db->query($sql);
            $count = $query->num_rows();
//        var_dump($count, $query->result_array());
            $data = array(
                'result' => SUCCESS,
                'count' => $count,
                'data' => $query->result_array()
            );
            return json_encode($data);
        } else {
            $body = (json_decode($body));
            if ($body->type === 'student') {
                if ($body->kecheng === 'zuowen') {
                    $sql = /** @lang text */
                        "SELECT a.id,a.name FROM ws_baseinfo AS a LEFT JOIN ws_class_zuowen AS b ON b.`studentId` = a.`id` WHERE b.`gradeId`=" . $body->grade . ' AND b.`classId`=' . $body->class;
                } elseif ($body->kecheng === 'aoshu') {
                    $sql = /** @lang text */
                        "SELECT a.id,a.name FROM ws_baseinfo AS a LEFT JOIN ws_class_aoshu AS b ON b.`studentId` = a.`id` WHERE b.`gradeId`=" . $body->grade . ' AND b.`classId`=' . $body->class;
                } elseif ($body->kecheng === 'shufa') {
                    $sql = /** @lang text */
                        "SELECT a.id,a.name FROM ws_baseinfo AS a LEFT JOIN ws_class_shufa AS b ON b.`studentId` = a.`id` WHERE b.`gradeId`=" . $body->grade . ' AND b.`classId`=' . $body->class;
                }

                $query = $this->db->query($sql);
                $count = $query->num_rows();
//                var_dump($query->result_array());
//                return;
                $uid = array('0');
                $name = array('请选择');
                $data = array(
                    'result' => SUCCESS,
                    'count' => $count,
                    'id' => array(),
                    'name' => array(array('id' => '0', 'name' => '请选择')),
                    'course' => array()
                );
                foreach ($query->result_array() as $key => $value) {
//                    array_push($data['data'], $value);
                    array_push($uid, $value['id']);
                    array_push($name, $value['name']);
                }
                $data['id'] = $uid;
                $data['name'] = $name;
                return json_encode($data);
            } elseif ($body->type === 'info') {
                $sql = /** @lang text */
                    "SELECT name,sex,school,grade,class,phone FROM ws_baseinfo WHERE id =" . $body->id;
                $query = $this->db->query($sql);
                $data = array(
                    'result' => ERROR,
                    'data' => array('name' => '', 'sex' => 0, 'phone' => ''),
                    'array' => array()
                );
                if ($query) {
                    $data['result'] = SUCCESS;
                    foreach ($query->result_array() as $key => $value) {
                        $data['data']['name'] = $value['name'];
                        $data['data']['sex'] = $value['sex'];
                        $data['data']['phone'] = $value['phone'];
                        array_push($data['array'], $value['school']);
                        array_push($data['array'], $value['grade']);
                        array_push($data['array'], $value['class']);
                    }
                }
                $data['course'] = $this->get_grade($body->id);
                return json_encode($data);
            }
        }
        return true;
    }

    function get_grade($id)
    {
//        SELECT * FROM ws_class_zuowen WHERE studentId = 1;
//SELECT * FROM ws_class_aoshu WHERE studentId = 1;
//SELECT * FROM ws_class_shufa WHERE studentId = 1;
        $sql = /** @lang text */
            "SELECT * FROM ws_class_zuowen WHERE studentId = " . $id;

        $query = $this->db->query($sql);
        $count = $query->num_rows();
        $data = array();
        if ($count) {
            $tmp = array();
            array_push($tmp, '1');
            array_push($tmp, $query->result_array()[0]['gradeId']);
            array_push($tmp, $query->result_array()[0]['classId']);
            array_push($data, $tmp);
//            var_dump('1', $query->result_array()[0]['gradeId'], $query->result_array()[0]['classId']);
        }

        $sql = /** @lang text */
            "SELECT * FROM ws_class_aoshu WHERE studentId = " . $id;
        $query = $this->db->query($sql);
        $count = $query->num_rows();
        if ($count) {
            $tmp = array();
            array_push($tmp, '2');
            array_push($tmp, $query->result_array()[0]['gradeId']);
            array_push($tmp, $query->result_array()[0]['classId']);
            array_push($data, $tmp);
//            var_dump('2', $query->result_array()[0]['gradeId'], $query->result_array()[0]['classId']);
        }

        $sql = /** @lang text */
            "SELECT * FROM ws_class_shufa WHERE studentId = " . $id;
        $query = $this->db->query($sql);
        $count = $query->num_rows();
        if ($count) {
            $tmp = array();
            array_push($tmp, '3');
            array_push($tmp, $query->result_array()[0]['gradeId']);
            array_push($tmp, $query->result_array()[0]['classId']);
            array_push($data, $tmp);
//            var_dump('3', $query->result_array()[0]['gradeId'], $query->result_array()[0]['classId']);
        }

        return $data;
    }
}